﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using SimpleData.ObjectModel.Design;
using SimpleData.ObjectModel;
using System.Text.RegularExpressions;
using System.Diagnostics.Contracts;
using System.Collections.ObjectModel;

namespace Globals.Types
{
	/// <summary>
	/// 
	/// </summary>
	public class TextFieldTypeDescritor : FieldTypeDescriptor<TextFieldType>
	{
	    public TextFieldTypeDescritor()
	        : base( new FieldTypeName( "Text" ) )
	    { }
	}

	public class TextFieldTypeDescriptorV2 : FieldTypeDescriptor
	{
		public List<string> Errors = new List<string>();

		public TextFieldTypeDescriptorV2()
			: base( new FieldTypeName( "Text" ) )
		{
			//this.Errors.Add<TextFieldType>( text => text.MaxLength, "O tamanho do campo deve ser menor que ..." );
		}


		public override IFieldTypeConverter CreateConverter( FieldDescriptor field )
		{
			//return new TextFieldTypeConverter( field )
			return null;
		}

		public override IRuleBuilder CreateRuleBuilder( Rule data )
		{
			//TextFieldType text = (TextFieldType)data.RuleData;
			//return new TextFieldRuleBuilder( text );
			return null;
		}


	}

	public class TextFieldType : IFieldTypeConverter, IRuleBuilder
	{
		private int maxLength = 0;

		public int MaxLength
		{
			get { return this.maxLength; }
			set
			{
				this.maxLength = Math.Max( value, 0 );
			}
		}

		#region IDataObjectStreamer Members

		public byte[] Serialize( object value )
		{
			return System.Text.Encoding.UTF8.GetBytes( ( string )value );
		}

		public object Deserialize( byte[] value )
		{
			if ( value == null )
				return String.Empty;
			else
				return System.Text.Encoding.UTF8.GetString( value );
		}

		#endregion

		#region IRuleBuilder Members

		public void BuildOn( ValidatorBuilder builder )
		{
			//builder.IfInputIsTypeOf<string>().IsValid();
			builder
				.Field()
				.ValidIf( input => input is string );

			builder
				.Field<String>()
				.ValidIf( input => ( input ?? String.Empty ).Length <= this.MaxLength || this.MaxLength == 0 );

			// Adicionar validação do tamanho máximo
			// TODO : Adicionar validação de tamanho da string
		}

		#endregion

		#region IFieldTypeConverter Members

		public Type GetDotNetType()
		{
			return typeof( string );
		}

		#endregion
	}

	public abstract class FieldTypeBase<TType> : IFieldTypeConverter, IRuleBuilder
	{



		/// <summary>
		/// 
		/// </summary>
		/// <param name="messages"></param>
		protected virtual void InitErrors( List<ErrorMessage> messages )
		{ }

		protected void AddError( Expression<Action> property )
		{
			string propertyName = property.GetPropertyName();
			var msg = new ErrorMessage() { Alias = propertyName };
		}

		/// <summary>
		/// 
		/// </summary>
		public ReadOnlyCollection<ErrorMessage> Errors
		{
			get;
			private set;
		}


		#region IRuleBuilder Members

		public void BuildOn( ValidatorBuilder builder )
		{ 
			
		}

		#endregion

		#region IFieldTypeConverter Members

		public virtual Type GetDotNetType()
		{
			return typeof( TType );
		}

		#endregion

		#region IDataObjectStreamer Members

		public virtual byte[] Serialize( object value )
		{
			if ( value != null )
			{
			}
			return new byte[] { };
		}

		public virtual object Deserialize( byte[] value )
		{
			if ( value != null && value.Length > 0 )
			{

			}
			return default( TType );
		}

		#endregion


		/// <summary>
		/// 
		/// </summary>
		public class ErrorMessage
		{
			public string DisplayName
			{ get; set; }

			public string Alias
			{ get; set; }

			public string MessageFormat
			{ get; set; }
		}
	}

	public class UrlFieldType : TextFieldType
	{

	}
}
